/*
File Name: Joystick.c
Written by: Sam Lee
Last Modify: November 03, 2009
*/

//Crystal= 20 MHz; Baud Rate = 115.2k; SPBRG = 2,

#include<p18F4520.h>
#include <usart.h>
#include <delays.h>

#pragma config WDT = OFF // turns the watchdog timer off
#pragma config PBADEN = OFF

void main()
{
  //Initializing ports
  PORTA = 0;
  PORTB = 0xFF;

  //Set RB0-RB3 as input 
  TRISB |= 0x0F;

  //Set RA0-RA3 as output
  TRISA &= 0xF0;
	
  //Serial Port Setting
  TRISC = 0x00;	
  OpenUSART(USART_TX_INT_OFF & 
			USART_RX_INT_OFF & 
			USART_ASYNCH_MODE & 
			USART_EIGHT_BIT &   
			USART_CONT_RX & 
			USART_BRGH_LOW, 2);
						 
  while(1)
  { 
    if(PORTBbits.RB0 == 0)
    {
      putrsUSART("8");    //robot drive forward 
      PORTA = 0x01;
    }
	else if(PORTBbits.RB1 == 0)
    {
      putrsUSART("2");    //robot drive back
      PORTA = 0x02;
    }
	else if(PORTBbits.RB2 == 0)
    {
      putrsUSART("6");    //robot drive right 
      PORTA = 0x04;
    }
    else if(PORTBbits.RB3 == 0)
    {
      putrsUSART("4");    //robot drive left 
      PORTA = 0x08;
	}
    else
    {
      putrsUSART("5");
      PORTA = 0;
    }
    Delay10KTCYx(100);
    //CloseUSART(); 
  }
}